#include <iostream>
#include <vector>
using namespace std;

int dp[1005][1005] = {};

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
bool valid(int x, int y, int m, int n) {
    if (x < 0 || x >= m || y < 0 || y >= n) return 0;
    return 1;
}

int dfs(int r, int c, const vector<vector<int>> &matrix) {
    if (dp[r][c] != -1) return dp[r][c];
    dp[r][c] = 1;
    int maxNeighbour = 0;
    for (int i = 0; i < 4; i++) {
        int fx = r + dx[i];
        int fy = c + dy[i];
        if (!valid(fx, fy, matrix.size(), matrix[0].size())) continue;
        if (matrix[fx][fy] <= matrix[r][c]) continue;
        maxNeighbour = max(maxNeighbour, dfs(fx, fy, matrix));
    }
    dp[r][c] += maxNeighbour;
    return dp[r][c];
}

int solve(vector<vector<int>> &matrix) {
    int m = matrix.size();
    int n = matrix[0].size();
    int ans = -1;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) { dp[i][j] = -1; }
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (dp[i][j] != -1) continue;
            dfs(i, j, matrix);
        }
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) { ans = max(ans, dp[i][j]); }
    }
    return ans;
}